home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / lisp / wgdb-42.lha / wgdb-4.2 / gdb / infptrace.c < prev    next >
C/C++ Source or Header  |  1992-09-11  |  11KB  |  421 lines

  1. /* Low level Unix child interface to ptrace, for GDB when running under Unix.
  2.    Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
  3.  
  4. This file is part of GDB.
  5.  
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program; if not, write to the Free Software
  18. Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20. #include <stdio.h>
  21. #include "defs.h"
  22. #include "param.h"
  23. #include "frame.h"
  24. #include "inferior.h"
  25. #include "target.h"
  26.  
  27. #ifdef USG
  28. #include <sys/types.h>
  29. #endif
  30.  
  31. #include <sys/param.h>
  32. #include <sys/dir.h>
  33. #include <signal.h>
  34. #include <sys/ioctl.h>
  35. #ifndef USG
  36. #include <sys/ptrace.h>
  37. #endif
  38.  
  39. #if !defined (PT_KILL)
  40. #define PT_KILL 8
  41. #define PT_STEP 9
  42. #define PT_CONTINUE 7
  43. #define PT_READ_U 3
  44. #define PT_WRITE_U 6
  45. #define PT_READ_I 1
  46. #define    PT_READ_D 2
  47. #define PT_WRITE_I 4
  48. #define PT_WRITE_D 5
  49. #endif /* No PT_KILL.  */
  50.  
  51. #ifndef PT_ATTACH
  52. #define PT_ATTACH PTRACE_ATTACH
  53. #endif
  54. #ifndef PT_DETACH
  55. #define PT_DETACH PTRACE_DETACH
  56. #endif
  57.  
  58. #include "gdbcore.h"
  59. #include <sys/user.h>        /* After a.out.h  */
  60. #include <sys/file.h>
  61. #include <sys/stat.h>
  62.  
  63. /* This function simply calls ptrace with the given arguments.  
  64.    It exists so that all calls to ptrace are isolated in this 
  65.    machine-dependent file. */
  66. int
  67. call_ptrace (request, pid, addr, data)
  68.      int request, pid, *addr, data;
  69. {
  70.   return ptrace (request, pid, addr, data);
  71. }
  72.  
  73. #ifdef DEBUG_PTRACE
  74. /* For the rest of the file, use an extra level of indirection */
  75. /* This lets us breakpoint usefully on call_ptrace. */
  76. #define ptrace call_ptrace
  77. #endif
  78.  
  79. /* This is used when GDB is exiting.  It gives less chance of error.*/
  80.  
  81. void
  82. kill_inferior_fast ()
  83. {
  84.   if (inferior_pid == 0)
  85.     return;
  86.   ptrace (PT_KILL, inferior_pid, 0, 0);
  87.   wait ((int *)0);
  88. }
  89.  
  90. void
  91. kill_inferior (args, from_tty)
  92.      char *args;
  93.      int from_tty;
  94. {
  95.   kill_inferior_fast ();
  96.   target_mourn_inferior ();
  97. }
  98.  
  99. /* Resume execution of the inferior process.
  100.    If STEP is nonzero, single-step it.
  101.    If SIGNAL is nonzero, give it that signal.  */
  102.  
  103. void
  104. child_resume (step, signal)
  105.      int step;
  106.      int signal;
  107. {
  108.   errno = 0;
  109.  
  110.   /* An address of (int *)1 tells ptrace to continue from where it was. 
  111.      (If GDB wanted it to start some other way, we have already written
  112.      a new PC value to the child.)  */
  113.  
  114.   if (step)
  115.     ptrace (PT_STEP, inferior_pid, (int *)1, signal);
  116.   else
  117.     ptrace (PT_CONTINUE, inferior_pid, (int *)1, signal);
  118.  
  119.   if (errno)
  120.     perror_with_name ("ptrace");
  121. }
  122.  
  123. #ifdef ATTACH_DETACH
  124. /* Nonzero if we are debugging an attached process rather than
  125.    an inferior.  */
  126. extern int attach_flag;
  127.  
  128. /* Start debugging the process whose number is PID.  */
  129. int
  130. attach (pid)
  131.      int pid;
  132. {
  133.   errno = 0;
  134.   ptrace (PT_ATTACH, pid, 0, 0);
  135.   if (errno)
  136.     perror_with_name ("ptrace");
  137.   attach_flag = 1;
  138.   return pid;
  139. }
  140.  
  141. /* Stop debugging the process whose number is PID
  142.    and continue it with signal number SIGNAL.
  143.    SIGNAL = 0 means just continue it.  */
  144.  
  145. void
  146. detach (signal)
  147.      int signal;
  148. {
  149.   errno = 0;
  150.   ptrace (PT_DETACH, inferior_pid, 1, signal);
  151.   if (errno)
  152.     perror_with_name ("ptrace");
  153.   attach_flag = 0;
  154. }
  155. #endif /* ATTACH_DETACH */
  156.  
  157. #if !defined (FETCH_INFERIOR_REGISTERS)
  158.  
  159. /* KERNEL_U_ADDR is the amount to subtract from u.u_ar0
  160.    to get the offset in the core file of the register values.  */
  161. #if defined (KERNEL_U_ADDR_BSD)
  162. /* Get kernel_u_addr using BSD-style nlist().  */
  163. CORE_ADDR kernel_u_addr;
  164. #include <a.out.gnu.h>        /* For struct nlist */
  165.  
  166. void
  167. _initialize_kernel_u_addr ()
  168. {
  169.   struct nlist names[2];
  170.  
  171.   names[0].n_un.n_name = "_u";
  172.   names[1].n_un.n_name = NULL;
  173.   if (nlist ("/vmunix", names) == 0)
  174.     kernel_u_addr = names[0].n_value;
  175.   else
  176.     fatal ("Unable to get kernel u area address.");
  177. }
  178. #endif /* KERNEL_U_ADDR_BSD.  */
  179.  
  180. #if defined (KERNEL_U_ADDR_HPUX)
  181. /* Get kernel_u_addr using HPUX-style nlist().  */
  182. CORE_ADDR kernel_u_addr;
  183.  
  184. struct hpnlist {      
  185.         char *          n_name;
  186.         long            n_value;  
  187.         unsigned char   n_type;   
  188.         unsigned char   n_length;  
  189.         short           n_almod;   
  190.         short           n_unused;
  191. };
  192. static struct hpnlist nl[] = {{ "_u", -1, }, { (char *) 0, }};
  193.  
  194. /* read the value of the u area from the hp-ux kernel */
  195. void _initialize_kernel_u_addr ()
  196. {
  197.     struct user u;
  198.     nlist ("/hp-ux", &nl);
  199.     kernel_u_addr = nl[0].n_value;
  200. }
  201. #endif /* KERNEL_U_ADDR_HPUX.  */
  202.  
  203. #if !defined (offsetof)
  204. #define offsetof(TYPE, MEMBER) ((unsigned long) &((TYPE *)0)->MEMBER)
  205. #endif
  206.  
  207. /* U_REGS_OFFSET is the offset of the registers within the u area.  */
  208. #if !defined (U_REGS_OFFSET)
  209. #define U_REGS_OFFSET \
  210.   ptrace (PT_READ_U, inferior_pid, \
  211.       (int *)(offsetof (struct user, u_ar0)), 0) - KERNEL_U_ADDR
  212. #endif
  213.  
  214. /* Registers we shouldn't try to fetch.  */
  215. #if !defined (CANNOT_FETCH_REGISTER)
  216. #define CANNOT_FETCH_REGISTER(regno) 0
  217. #endif
  218.  
  219. /* Fetch one register.  */
  220.  
  221. static void
  222. fetch_register (regno)
  223.      int regno;
  224. {
  225.   register unsigned int regaddr;
  226.   char buf[MAX_REGISTER_RAW_SIZE];
  227.   char mess[128];                /* For messages */
  228.   register int i;
  229.  
  230.   /* Offset of registers within the u area.  */
  231.   unsigned int offset;
  232.  
  233.   if (CANNOT_FETCH_REGISTER (regno))
  234.     {
  235.       bzero (buf, REGISTER_RAW_SIZE (regno));    /* Supply zeroes */
  236.       supply_register (regno, buf);
  237.       return;
  238.     }
  239.  
  240.   offset = U_REGS_OFFSET;
  241.  
  242.   regaddr = register_addr (regno, offset);
  243.   for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof (int))
  244.     {
  245.       errno = 0;
  246.       *(int *) &buf[i] = ptrace (PT_READ_U, inferior_pid, (int *)regaddr, 0);
  247.       regaddr += sizeof (int);
  248.       if (errno != 0)
  249.     {
  250.       sprintf (mess, "reading register %s (#%d)", reg_names[regno], regno);
  251.       perror_with_name (mess);
  252.     }
  253.     }
  254.   supply_register (regno, buf);
  255. }
  256.  
  257.  
  258. /* Fetch all registers, or just one, from the child process.  */
  259.  
  260. void
  261. fetch_inferior_registers (regno)
  262.      int regno;
  263. {
  264.   if (regno == -1)
  265.     for (regno = 0; regno < NUM_REGS; regno++)
  266.       fetch_register (regno);
  267.   else
  268.     fetch_register (regno);
  269. }
  270.  
  271. /* Registers we shouldn't try to store.  */
  272. #if !defined (CANNOT_STORE_REGISTER)
  273. #define CANNOT_STORE_REGISTER(regno) 0
  274. #endif
  275.  
  276. /* Store our register values back into the inferior.
  277.    If REGNO is -1, do this for all registers.
  278.    Otherwise, REGNO specifies which register (so we can save time).  */
  279.  
  280. int
  281. store_inferior_registers (regno)
  282.      int regno;
  283. {
  284.   register unsigned int regaddr;
  285.   char buf[80];
  286.   extern char registers[];
  287.   register int i;
  288.   int result = 0;
  289.  
  290.   unsigned int offset = U_REGS_OFFSET;
  291.  
  292.   if (regno >= 0)
  293.     {
  294.       regaddr = register_addr (regno, offset);
  295.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  296.     {
  297.       errno = 0;
  298.       ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  299.           *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  300.       if (errno != 0)
  301.         {
  302.           sprintf (buf, "writing register number %d(%d)", regno, i);
  303.           perror_with_name (buf);
  304.           result = -1;
  305.         }
  306.       regaddr += sizeof(int);
  307.     }
  308.     }
  309.   else
  310.     {
  311.       for (regno = 0; regno < NUM_REGS; regno++)
  312.     {
  313.       if (CANNOT_STORE_REGISTER (regno))
  314.         continue;
  315.       regaddr = register_addr (regno, offset);
  316.       for (i = 0; i < REGISTER_RAW_SIZE (regno); i += sizeof(int))
  317.         {
  318.           errno = 0;
  319.           ptrace (PT_WRITE_U, inferior_pid, (int *)regaddr,
  320.               *(int *) ®isters[REGISTER_BYTE (regno) + i]);
  321.           if (errno != 0)
  322.         {
  323.           sprintf (buf, "writing register number %d(%d)", regno, i);
  324.           perror_with_name (buf);
  325.           result = -1;
  326.         }
  327.           regaddr += sizeof(int);
  328.         }
  329.     }
  330.     }
  331.   return result;
  332. }
  333. #endif /* !defined (FETCH_INFERIOR_REGISTERS).  */
  334.  
  335. /* NOTE! I tried using PTRACE_READDATA, etc., to read and write memory
  336.    in the NEW_SUN_PTRACE case.
  337.    It ought to be straightforward.  But it appears that writing did
  338.    not write the data that I specified.  I cannot understand where
  339.    it got the data that it actually did write.  */
  340.  
  341. /* Copy LEN bytes to or from inferior's memory starting at MEMADDR
  342.    to debugger memory starting at MYADDR.   Copy to inferior if
  343.    WRITE is nonzero.
  344.   
  345.    Returns the length copied, which is either the LEN argument or zero.
  346.    This xfer function does not do partial moves, since child_ops
  347.    doesn't allow memory operations to cross below us in the target stack
  348.    anyway.  */
  349.  
  350. int
  351. child_xfer_memory (memaddr, myaddr, len, write, target)
  352.      CORE_ADDR memaddr;
  353.      char *myaddr;
  354.      int len;
  355.      int write;
  356.      struct target_ops target;        /* ignored */
  357. {
  358.   register int i;
  359.   /* Round starting address down to longword boundary.  */
  360.   register CORE_ADDR addr = memaddr & - sizeof (int);
  361.   /* Round ending address up; get number of longwords that makes.  */
  362.   register int count
  363.     = (((memaddr + len) - addr) + sizeof (int) - 1) / sizeof (int);
  364.   /* Allocate buffer of that many longwords.  */
  365.   register int *buffer = (int *) alloca (count * sizeof (int));
  366.  
  367.   if (write)
  368.     {
  369.       /* Fill start and end extra bytes of buffer with existing memory data.  */
  370.  
  371.       if (addr != memaddr || len < (int)sizeof (int)) {
  372.     /* Need part of initial word -- fetch it.  */
  373.         buffer[0] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  374.       }
  375.  
  376.       if (count > 1)        /* FIXME, avoid if even boundary */
  377.     {
  378.       buffer[count - 1]
  379.         = ptrace (PT_READ_I, inferior_pid,
  380.               (int *)(addr + (count - 1) * sizeof (int)), 0);
  381.     }
  382.  
  383.       /* Copy data to be written over corresponding part of buffer */
  384.  
  385.       bcopy (myaddr, (char *) buffer + (memaddr & (sizeof (int) - 1)), len);
  386.  
  387.       /* Write the entire buffer.  */
  388.  
  389.       for (i = 0; i < count; i++, addr += sizeof (int))
  390.     {
  391.       errno = 0;
  392.       ptrace (PT_WRITE_D, inferior_pid, (int *)addr, buffer[i]);
  393.       if (errno)
  394.         {
  395.           /* Using the appropriate one (I or D) is necessary for
  396.          Gould NP1, at least.  */
  397.           errno = 0;
  398.           ptrace (PT_WRITE_I, inferior_pid, (int *)addr, buffer[i]);
  399.         }
  400.       if (errno)
  401.         return 0;
  402.     }
  403.     }
  404.   else
  405.     {
  406.       /* Read all the longwords */
  407.       for (i = 0; i < count; i++, addr += sizeof (int))
  408.     {
  409.       errno = 0;
  410.       buffer[i] = ptrace (PT_READ_I, inferior_pid, (int *)addr, 0);
  411.       if (errno)
  412.         return 0;
  413.       QUIT;
  414.     }
  415.  
  416.       /* Copy appropriate bytes out of the buffer.  */
  417.       bcopy ((char *) buffer + (memaddr & (sizeof (int) - 1)), myaddr, len);
  418.     }
  419.   return len;
  420. }
  421.